/*
 * @Description: * 
 * @Date: 2021-01-06 22:34:07
 * @LastEditTime: 2021-01-11 21:05:05
 * @Author: Ji YingHao
 * @FilePath: \iview\plugins\service.js
 */

import { Message } from 'iview'
import axios from 'axios'
import CONFIG from '@/config'
import API from "@/api/index";

axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers.post['Content-Type'] = 'text/plain;charset=UTF-8'
// axios.defaults.headers['Authori-zation'] = '';   //主要是这里
let service = axios.create({
    baseURL: CONFIG.baseURL,
    timeout: 30000
})



// 请求拦截 可在请求头中加入token等
service.interceptors.request.use(config => {
    config.headers['Authori-zation'] = API.getCookie('key');
    return config
}, error => {
    return Promise.reject(error)
})

// 

// 响应拦截 对响应消息作初步的处理
service.interceptors.response.use(res => {
    // if (res.data.code !== 200) {
    //     Message.error(res.data.message || '出错了');
    // }
    console.log('接口：', res.data);
    return Promise.resolve(res.data);
}, error => {
    console.log('接口报错：', error);
    if (error.response) {
        switch (error.response.status) {
            case 400: {
                Message.error('400' || '出错了');
            }
            case 404: {
                Message.error('404' || '出错了');
            }
            default:
                Message.error(error.response.status || '出错了');
        }
    }
    return Promise.reject(error)
})

export default service